package com.bingxu.flink.chapter02.sink;

import com.bingxu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.jdbc.JdbcSink;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import org.apache.flink.streaming.connectors.redis.RedisSink;
import org.apache.flink.streaming.connectors.redis.common.config.FlinkJedisConfigBase;
import org.apache.flink.streaming.connectors.redis.common.config.FlinkJedisPoolConfig;
import org.apache.flink.streaming.connectors.redis.common.mapper.RedisCommand;
import org.apache.flink.streaming.connectors.redis.common.mapper.RedisCommandDescription;
import org.apache.flink.streaming.connectors.redis.common.mapper.RedisMapper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * @author :BingXu
 * @description :TODO
 * @date :2021/8/10 11:48
 * @modifier :
 */

public class Trans_CustomSink {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> streamDS = env.socketTextStream("localhost", 9999);
        SingleOutputStreamOperator<WaterSensor> waterSensorDS = streamDS.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String s) throws Exception {
                String[] fields = s.split(",");
                return new WaterSensor(fields[0], Long.parseLong(fields[1]), Integer.parseInt(fields[2]));
            }
        });
        MySink mySink = new MySink();
        waterSensorDS.addSink(mySink);

        env.execute();
    }

    public static class MySink extends RichSinkFunction<WaterSensor> {
        Connection conn = null;
        PreparedStatement pstSQL = null;

        /**
         *  获取一个连接在开始之前
         * @param parameters
         * @throws Exception
         */
        @Override
        public void open(Configuration parameters) throws Exception {
            conn = DriverManager.getConnection("jdbc:mysql://hadoop102:3306/flinktest", "root", "root123");
            pstSQL = conn.prepareStatement("insert into sensor values (?,?,?) on duplicate key  update vc=? ");
        }

        @Override
        public void close() throws Exception {
            if (pstSQL != null) {
                pstSQL.close();
            }
            if (conn != null) {
                conn.close();
            }
        }

        /**
         * 处理传入element如何写入数据库的逻辑处理
         * @param value
         * @param context
         * @throws Exception
         */
        @Override
        public void invoke(WaterSensor value, Context context) throws Exception {
            pstSQL.setString(1,value.getId());
            pstSQL.setLong(2,value.getTs());
            pstSQL.setInt(3,value.getVc());
            pstSQL.setInt(4,value.getVc());
            pstSQL.execute();
        }
    }
}
